﻿using System.Drawing;
using System.Windows.Forms;
using System.Collections.Generic;
using System;

namespace Tron
{
    public class Moto
    {
        private int posX;
        private int posY;
        private int currentDirection;
        private Color color;
        private PictureBox pictureMoto;
        private Board board;
        private SolidBrush brush;
        private List<Rectangle> motoRec;
        private Size size;
        private String dirImagePath;

        public Moto()
        {
            motoRec = new List<Rectangle>();
            pictureMoto = new PictureBox
            {
                Location = new Point(0,0),
                Size = new Size(Constantes.tall, Constantes.tall)
            };
        }

        public Moto(Point location, int direction, Color color, String dirImagePath, Board board)
        {
            currentDirection = direction;
            this.color = color;
            this.board = board;
            this.dirImagePath = dirImagePath;

            brush = new SolidBrush(color);
            motoRec = new List<Rectangle>();
            size = new Size(Constantes.tall, Constantes.tall);

            //PictureBox
            pictureMoto = new PictureBox
            {
                Location = location,
                Size = new Size(Constantes.tall, Constantes.tall)
            };

            board.Matrix[location.X / Constantes.tall, location.Y / Constantes.tall].State = false;

            this.loadImage();
        }

        public void drawLine(Graphics paper)
        {
            foreach (Rectangle rec in motoRec)
            {
                paper.FillRectangle(brush, rec);
            }
        }

        /**
         * Detecte les collisions de la moto.
         * Collision plateau || collision entre motos.
         * 
         * return true si collision, false si pas de collision.
         */
        public Boolean collide(Board board)
        {
            Boolean collision = false;
            posX = pictureMoto.Location.X / Constantes.tall; // Emplacement actuel de la moto
            posY = pictureMoto.Location.Y / Constantes.tall;

            // Plateau
            if ((currentDirection == 1 && posY == 0) || (currentDirection == 2 && posY == Constantes.Ymatrice - 1) ||
               (currentDirection == 3 && posX == Constantes.Xmatrice - 1) || (currentDirection == 4 && posX == 0))
                collision = true;
            // Collisin selon l'état de la prochaine case
            else if ((currentDirection == 1 && board.Matrix[posX, posY - 1].State != true) ||
               (currentDirection == 2 && board.Matrix[posX, posY + 1].State != true) ||
               (currentDirection == 3 && board.Matrix[posX + 1, posY].State != true) ||
               (currentDirection == 4 && board.Matrix[posX - 1, posY].State != true))
                collision = true;

            return collision;
        }

        public void loadImage()
        {
            String imagePath = (System.Environment.CurrentDirectory);
            if (imagePath.EndsWith("\\bin\\Debug"))
            {
                imagePath = imagePath.Replace("\\bin\\Debug", "");
            }
            if (imagePath.EndsWith("\\bin\\Release"))
            {
                imagePath = imagePath.Replace("\\bin\\Release", "");
            }

            if (currentDirection == (int)Constantes.directions.up)
                imagePath = imagePath + "\\Images\\" + dirImagePath + "up.png";
            else if (currentDirection == (int)Constantes.directions.down)
                imagePath = imagePath + "\\Images\\" + dirImagePath + "down.png";
            else if (currentDirection == (int)Constantes.directions.right)
                imagePath = imagePath + "\\Images\\" + dirImagePath + "right.png";
            else if (currentDirection == (int)Constantes.directions.left)
                imagePath = imagePath + "\\Images\\" + dirImagePath + "left.png";

            pictureMoto.Image = Image.FromFile(imagePath);
        }

        public void moveOn(Board board)
        {
            posX = pictureMoto.Location.X / Constantes.tall;
            posY = pictureMoto.Location.Y / Constantes.tall;
            Point pointRec = new Point(pictureMoto.Location.X, pictureMoto.Location.Y);

            if (currentDirection == (int)Constantes.directions.up)
            {
                pictureMoto.Location = board.Matrix[posX, posY - 1].Picture.Location;
            }
            else if (currentDirection == (int)Constantes.directions.down)
            {
                pictureMoto.Location = board.Matrix[posX, posY + 1].Picture.Location;
            }
            else if (currentDirection == (int)Constantes.directions.right)
            {
                pictureMoto.Location = board.Matrix[posX + 1, posY].Picture.Location;
            }
            else if (currentDirection == (int)Constantes.directions.left)
            {
                pictureMoto.Location = board.Matrix[posX - 1, posY].Picture.Location;
            }                

            board.Matrix[posX, posY].Picture.Dispose();
            motoRec.Add(new Rectangle(pointRec, size));
            board.Matrix[pictureMoto.Location.X / Constantes.tall, pictureMoto.Location.Y / Constantes.tall].State = false;
        }

        public int CurrentDirection
        {
            get
            {
                return currentDirection;
            }
            set
            {
                currentDirection = value;
            }
        }

        public PictureBox PictureMoto
        {
            get
            {
                return pictureMoto;
            }
            set
            {
                pictureMoto = value;
            }
        }

        public List<Rectangle> MotoRec
        {
            get
            {
                return motoRec;
            }
            set
            {
                motoRec = value;
            }
        }
    }
}
